﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Practices.Unity;
using Vroom.TableListener.Container;
using Vroom.TableListener.ViewModel;
using Vroom.TableListener.Views;


namespace Vroom.TableListener
{
	public partial class App : Application
	{

		protected override void OnStartup(StartupEventArgs e)
		{

			/*
			 * http://www.codeproject.com/Articles/132966/Exploring-Reactive-Extensions-Rx-through-Twitter-a
			 *
			 * http://www.codeproject.com/Articles/359157/Using-Reactive-Extensions-for-Streaming-Data-from
			 * 
			 * http://blogs.msdn.com/b/flaviencharlon/archive/2012/08/06/task-lt-t-gt-vs-iobservable-lt-t-gt-when-to-use-what.aspx
			 * 
			 * http://www.aboutmycode.com/reactive-extensions/using-reactive-extensions-for-streaming-data-from-database/
			 * 
			 * http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples
			 * 
			 */


			//Reg Types
			RegisterTypes();

			//Global Exception Handlers
			AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnAppDomainUnhandledException);
			Dispatcher.UnhandledException += OnDispatcherUnhandledException;
			Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
			TaskScheduler.UnobservedTaskException += new EventHandler<UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);

			//Create & Display MainView
			MainView mainWindow = TypeContainer.Instance.Resolve<MainView>();
			mainWindow.Show();
		}



		private void RegisterTypes()
		{
			TypeContainer.Register();
		}


		#region Exception Handlers

		void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			Exception ex = (Exception)e.ExceptionObject;

			if (ex != null)
			{
				string errorMessage = string.Format("An unhandled exception occurred: {0}", ex.Message);
				MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			}
			else
			{
				string errorMessage = "An unhandled exception occurred, can not be cast...";
				MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			}
		}


		void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
		{
			string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
			MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			e.Handled = true;
		}


		void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
		{
			string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
			MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			e.Handled = true;
		}


		void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
		{
			string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
			MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
		}

		#endregion
	}
}
